package com.cn.wxwinnie.java.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author 潇湘暮雨 E-mail:wxwinnie@hotmail.com
 * 
 * @version 创建时间：2014年3月25日 下午6:46:21
 * 
 */
public class BineryFileWR {

	public static byte[] readFile(String fpath) throws IOException {
		File f = new File(fpath);
		InputStream is = new FileInputStream(f);
		// f.renameTo(dest);
		int b = 0;
		int n = 0;
		int fileLength = is.available();
		byte[] content = new byte[fileLength];
		while (-1 != (b = is.read())) {// 每次读取一个byte
			content[n] = (byte) b;
			n++;
			// System.out.print(b + " ");
		}
		is.close();

		return content;
	}

	public static void writeFile(String fpath, byte[] fileContent)
			throws IOException {
		File f = new File(fpath);
		OutputStream os = new FileOutputStream(f);
		os.write(fileContent);
		os.close();
	}

	public static void writeFile(String fpath, char[] fileContent)
			throws IOException {
		int length = fileContent.length;
		byte[] bytes = new byte[length];
		for (int i = 0; i < length; i++) {
			bytes[i] = (byte) fileContent[i];
		}

		writeFile(fpath, bytes);
	}
}
